import { _decorator, Component, Node, RigidBody2D, Sprite, ERigidBody2DType,BoxCollider2D   } from 'cc';
import ResLoad from "db://assets/longTime/ResLoad";
import {pandaStatusEnum, pathEnum} from "db://assets/Enum";
import {PandaManager} from "db://assets/Script/PandaManager";
const { ccclass, property } = _decorator;
interface dataManagerType {
    data: Node,
    x: number
    y: number
}

@ccclass('RedPackManager')
export class RedPackManager extends Component {
    // 红包数量
    redPackNumber:number = 10
    // 红包列表
    redPackList:Array<dataManagerType> = []
    // 目标位置Y
    targetY:number = -1500
    // 红包移动速度
    moveSpeed:number = 10
    // 红包横向位置
    redPackPosArray:Array<number> = [-230,-50,150,280]
    async update(deltaTime: number) {
        for (let i = 0; i < this.redPackList.length; i++) {
            const argument = this.redPackList[i];
            if (argument.data.isValid) {
                this.move(i)
                argument.data.setPosition(argument.x,argument.y)
                if(argument.y === this.targetY) {
                    argument.data.destroy();
                }
            }
            // 最后一个红包被销毁
            if(!argument.data.isValid && i === this.redPackList.length - 1) {
                // todo 会触发多次问题待解决
                this.redPackList = []
                const panda = this.node.parent.getChildByName(pathEnum.PANDA)
                const sprite = panda.getComponent(Sprite)
                sprite.spriteFrame = await ResLoad.Instance.load(`panda/panda-${pandaStatusEnum.FULL}/spriteFrame`)
                // 关闭熊猫移动监听
                panda.off(Node.EventType.TOUCH_MOVE);
                console.log(`游戏结束，接到了${ panda.getComponent(PandaManager).pickUpNumber }个`)
            }
        }
    }
    // 播放红包下落
    play() {
        // 以秒为单位的时间间隔
        let interval = 0.5;
        // 重复次数
        let repeat = this.redPackNumber - 1;
        // 开始延时
        let delay = 3;
        this.schedule(function() {
            const randomPos = Math.floor(Math.random() * 4)
            this.createdRedPack(this.redPackPosArray[randomPos])
        }, interval, repeat, delay);
    }
    // 创建红包
    async createdRedPack(pos: number) {
        const startY = 0
        const redPack = new Node()
        redPack.setPosition(pos,0)
        const randomPack = Math.floor(Math.random() * 3) + 1
        const redPackSprite = redPack.addComponent(Sprite)
        const src = `pack-${randomPack}`
        redPackSprite.spriteFrame = await ResLoad.Instance.load(`redPack/${src}/spriteFrame`)
        const rigidBody2D = redPack.addComponent(RigidBody2D)
        rigidBody2D.type= ERigidBody2DType.Animated
        redPack.addComponent(BoxCollider2D)
        redPack.setParent(this.node)
        this.redPackList.push({data:redPack,x:pos,y:startY})
    }
    // 红包移动
    move(target:number) {
        const argument = this.redPackList[target];
        if (argument.y > this.targetY) {
            argument.y -= this.moveSpeed
        } else if(Math.abs(argument.y - this.targetY) < 5){
            argument.y = this.targetY
        }
    }
}


